home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Interapplication Communication / MenuScripter 4.0 / Sources / MSResultWind.c < prev    next >
Encoding:
Text File  |  1996-07-09  |  11.4 KB  |  434 lines  |  [TEXT/CWIE]

  1. // MSResultWind.c
  2. //
  3. // Written by Don Swatman and Greg Sutton
  4. // ©Apple Computer Inc 1996, all rights reserved.
  5.     
  6. #include "MSResultWind.h"
  7.  
  8. #ifdef THINK_C
  9.     #include "PLStrs.h"
  10. #else
  11.     #include <PLStringFuncs.h>
  12. #endif
  13. #include <ASRegistry.h>
  14.  
  15. #include "MSWindow.h"
  16. #include "MSAETextUtils.h"
  17. #include "MSAEWindowUtils.h"
  18. #include "MSAEDelete.h"
  19. #include "MSAESetData.h"
  20. #include "MSAECreate.h"
  21. #include "MSAESelect.h"
  22. #include "MSMain.h"
  23.  
  24.  
  25.     // Prototypes
  26. OSErr            AddPStrToResults( StringPtr theStringPtr );
  27. OSErr            AddDescToResults( AEDesc* theDesc );
  28. static OSErr    EndOfDocumentTextToken( WindowPtr theWindow, TextToken* theToken );
  29. OSAError        TextDescUsingOSADisplay( AEDesc* theDesc, AEDesc* theResult );
  30. OSErr            SelectErrorRange( AEDesc* theRangeDesc, DPtr theDoc );
  31.  
  32.  
  33.     // Globals
  34. extern ComponentInstance    gScriptingComponent;
  35.  
  36. WindowPtr                     gPResultWind = NULL;
  37.  
  38.  
  39. OSErr    OpenResultWind( void )
  40. {        
  41.     DPtr        myDoc;
  42.  
  43.     if ( ! gPResultWind )
  44.     {
  45.         myDoc = NewDocument(false, (WindowPtr)-1L );
  46.         myDoc->windowType = kResultsWind;
  47.         SetWTitle(myDoc->theWindow, "\presults" );
  48.         
  49.         gPResultWind = myDoc->theWindow;
  50.     }
  51.     else
  52.     {
  53.         ShowMSWindow( gPResultWind );
  54.         SelectWindow( gPResultWind );
  55.     }
  56.  
  57.     return noErr;
  58. }
  59.  
  60. Boolean IsThereAResultWind( void )
  61. {
  62.     WindowPtr    aWindow = GetResultsWindPtr( );
  63.  
  64.     if ( aWindow && ((WindowPeek)aWindow)->visible )
  65.         return ( true );
  66.     else
  67.         return ( false );
  68. }
  69.  
  70. Boolean IsThisResultWind( WindowPtr theWind )
  71. {
  72.     return ( theWind == GetResultsWindPtr( ) );
  73. }
  74.  
  75. void CloseResultWind( WindowPtr theWind )
  76. {
  77.     if ( IsThisResultWind( theWind ) )
  78.         HideMSWindow( theWind );
  79. }
  80.  
  81. WindowPtr GetResultsWindPtr( void )
  82. {
  83.     if ( ! gPResultWind )    // Just hasn't been accessed yet - usually always around
  84.     {                        //  just hidden.
  85.         OpenResultWind( );
  86.         CloseResultWind( gPResultWind );    // Just hides it
  87.     }
  88.  
  89.     return gPResultWind;
  90. }
  91.  
  92. DPtr GetResultsDoc( void )
  93. {
  94.     return DPtrFromWindowPtr( GetResultsWindPtr( ) );
  95. }
  96.  
  97.  
  98. OSErr    DisplayDescResult( WindowPtr theWindow, AEDesc* theTextDesc, DPtr theDoc, OSErr theErr )
  99. {
  100.      WindowPropToken    aToken;
  101.      OSErr            err;
  102.      
  103.      if ( errOSAScriptError == theErr )
  104.          return (OSErr)DisplayOSAScriptError( theDoc );
  105.            
  106.     if ( ! theWindow )
  107.         return noErr;
  108.  
  109.     aToken.tokenWindowToken.tokenWindow = theWindow;
  110.  
  111.             // If the descriptor is NULL then clear the window
  112.     if ( typeNull == theTextDesc->descriptorType )
  113.     {
  114.         err = TextDescFromDocumentToken( &aToken.tokenWindowToken, theTextDesc );
  115.         
  116.         if ( noErr == err )
  117.             err = DeleteDesc( theTextDesc );
  118.         
  119.         (void)AEDisposeDesc( theTextDesc );
  120.     }
  121.     else    // Set the text to that provided
  122.     {
  123.         aToken.tokenProperty = pText;        // All of the text in document
  124.         
  125.         err = SetDocumentTokenProperty( &aToken, theTextDesc );
  126.     }
  127.     
  128.     return err;
  129. }
  130.  
  131. OSErr    DisplayOSAIDResult( WindowPtr theWindow, OSAID theOSAID, DPtr theDoc, OSErr theErr )
  132. {
  133.     AEDesc        textDesc = { typeNull, NULL };
  134.     OSErr        err;
  135.      
  136.      if ( errOSAScriptError == theErr )
  137.          return (OSErr)DisplayOSAScriptError( theDoc );
  138.  
  139.     err = OSADisplay( gScriptingComponent, theOSAID, typeStyledText,
  140.                                                 kOSAModeNull, &textDesc );
  141.     if ( noErr != err ) goto done;
  142.     
  143.     err = DisplayDescResult( theWindow, &textDesc, theDoc, theErr );
  144.                                                 
  145. done:
  146.     (void)AEDisposeDesc( &textDesc );
  147.     
  148.     return err;
  149. }
  150.  
  151.  
  152. OSAError    DisplayOSAScriptError( DPtr theDoc )
  153. {
  154.     AEDesc        applicationDesc = { typeNull, NULL },
  155.                 messageDesc = { typeNull, NULL },
  156.                 numberDesc = { typeNull, NULL },
  157.                 typeDesc = { typeNull, NULL },
  158.                 objectDesc = { typeNull, NULL },
  159.                 partialDesc = { typeNull, NULL },
  160.                 rangeDesc = { typeNull, NULL },
  161.                 aDesc = { typeNull, NULL };
  162.     Str255        aPStr;
  163.     OSAError    anErr;
  164.  
  165.     (void)OSAScriptError( gScriptingComponent, kOSAErrorApp, typeChar, &applicationDesc );
  166.     (void)OSAScriptError( gScriptingComponent, kOSAErrorMessage, typeChar, &messageDesc );
  167.     (void)OSAScriptError( gScriptingComponent, kOSAErrorNumber, typeShortInteger, &numberDesc );
  168.     (void)OSAScriptError( gScriptingComponent, kOSAErrorExpectedType, typeWildCard, &typeDesc );
  169.     (void)OSAScriptError( gScriptingComponent, kOSAErrorOffendingObject, typeWildCard, &objectDesc );
  170.     (void)OSAScriptError( gScriptingComponent, kOSAErrorPartialResult, typeWildCard, &partialDesc );
  171.     (void)OSAScriptError( gScriptingComponent, kOSAErrorRange, typeOSAErrorRange, &rangeDesc );
  172.  
  173.         // Select the range before bringing results document forward
  174.     anErr = SelectErrorRange( &rangeDesc, theDoc );
  175.     
  176.         // This calls a script that is executed through OSA so we need
  177.         //  to get all the script errors before calling.
  178.     if ( ! IsVisible( GetResultsWindPtr( ) ) )
  179.         DoMenuItem( mscriptID, cResultWindow );    // Go through script on menu item
  180.     else
  181.         SelectWindow( GetResultsWindPtr( ) );
  182.     
  183.     PLstrcpy( aPStr, "\pError" );
  184.     anErr = AECreateDesc( typeChar, (Ptr)&aPStr[1], aPStr[0], &aDesc );
  185.     if ( noErr != anErr ) goto done;
  186.         // Just blat it straight into the results document
  187.         //  deleting anything that's there.
  188.     anErr = DisplayDescResult( GetResultsWindPtr( ), &aDesc, NULL, noErr );
  189.     if ( noErr != anErr ) goto done;
  190.     
  191.         // Add the application name
  192.     if ( typeNull != applicationDesc.descriptorType )
  193.     {
  194.         anErr = AddPStrToResults( "\p in application " );
  195.         if ( noErr != anErr ) goto done;
  196.         anErr = AddDescToResults( &applicationDesc );
  197.         if ( noErr != anErr ) goto done;
  198.     }
  199.     
  200.     if ( typeNull != messageDesc.descriptorType )
  201.     {
  202.         anErr = AddPStrToResults( "\p\r\rMessage :  " );
  203.         if ( noErr != anErr ) goto done;
  204.         anErr = AddDescToResults( &messageDesc );
  205.         if ( noErr != anErr ) goto done;
  206.     }
  207.     
  208.     if ( typeNull != numberDesc.descriptorType )
  209.     {
  210.         anErr = AddPStrToResults( "\p\r\rNumber :  " );
  211.         if ( noErr != anErr ) goto done;
  212.         anErr = AddDescToResults( &numberDesc );
  213.         if ( noErr != anErr ) goto done;
  214.     }
  215.     
  216.     if ( typeNull != typeDesc.descriptorType )
  217.     {
  218.         anErr = AddPStrToResults( "\p\r\rType :  " );
  219.         if ( noErr != anErr ) goto done;
  220.         anErr = AddDescToResults( &typeDesc );    // Try and use OSADisplay() to get terminology
  221.         if ( noErr != anErr ) goto done;        //  for the type.
  222.     }
  223.     
  224.     if ( typeNull != objectDesc.descriptorType )
  225.     {
  226.         anErr = AddPStrToResults( "\p\r\rOffending Object :  " );
  227.         if ( noErr != anErr ) goto done;
  228.         anErr = AddDescToResults( &objectDesc );
  229.         if ( noErr != anErr ) goto done;
  230.     }
  231.     
  232.     if ( typeNull != partialDesc.descriptorType )
  233.     {
  234.         anErr = AddPStrToResults( "\p\r\rPartial Result :  " );
  235.         if ( noErr != anErr ) goto done;
  236.         anErr = AddDescToResults( &partialDesc );
  237.         if ( noErr != anErr ) goto done;
  238.     }
  239.     
  240. done:
  241.     (void)AEDisposeDesc( &applicationDesc );
  242.     (void)AEDisposeDesc( &messageDesc );
  243.     (void)AEDisposeDesc( &numberDesc );
  244.     (void)AEDisposeDesc( &typeDesc );
  245.     (void)AEDisposeDesc( &objectDesc );
  246.     (void)AEDisposeDesc( &partialDesc );
  247.     (void)AEDisposeDesc( &rangeDesc );
  248.     (void)AEDisposeDesc( &aDesc );
  249.  
  250.     return anErr;
  251. }
  252.  
  253. OSErr    SelectErrorRange( AEDesc* theRangeDesc, DPtr theDoc )
  254. {
  255.     AEDesc        aRecordDesc = { typeNull, NULL };
  256.     DescType    aType;
  257.     short        errorStart,
  258.                 errorEnd;
  259.     Size        actSize;
  260.     TextToken    aTextToken;
  261.     OSErr        anErr;
  262.  
  263.     if ( ! theDoc )
  264.         return noErr;
  265.     
  266.     anErr = AECoerceDesc( theRangeDesc, typeAERecord, &aRecordDesc );
  267.     if ( noErr != anErr ) goto done;
  268.  
  269.     anErr = AEGetKeyPtr( &aRecordDesc, keyOSASourceStart, typeShortInteger,
  270.                         &aType, (Ptr)&errorStart, sizeof( errorStart ), &actSize );
  271.     if ( noErr != anErr ) goto done;
  272.                                             
  273.     anErr = AEGetKeyPtr( &aRecordDesc, keyOSASourceEnd, typeShortInteger,
  274.                             &aType, (Ptr)&errorEnd, sizeof( errorEnd ), &actSize );
  275.     if ( noErr != anErr ) goto done;
  276.  
  277.     aTextToken.tokenWindow = theDoc->theWindow;    
  278.     aTextToken.tokenOffset = errorStart + 1;
  279.     aTextToken.tokenLength = errorEnd - errorStart;
  280.     
  281.     anErr = SelectTextToken( &aTextToken );
  282.  
  283.  
  284. done:
  285.     (void)AEDisposeDesc( &aRecordDesc );
  286.     
  287.     return anErr;
  288. }
  289.  
  290. OSErr    AddPStrToResults( StringPtr theStringPtr )
  291. {
  292.     AEDesc    aTextDesc = { typeNull, NULL };
  293.     OSErr    anErr;
  294.     
  295.     anErr = AECreateDesc( typeChar, (Ptr)&theStringPtr[1], theStringPtr[0], &aTextDesc );
  296.     if ( noErr != anErr ) goto done;
  297.     
  298.     anErr = AddDescToResults( &aTextDesc );
  299.     
  300. done:
  301.     (void)AEDisposeDesc( &aTextDesc );
  302.  
  303.     return anErr;
  304. }
  305.  
  306. OSErr    AddDescToResults( AEDesc* theDesc )
  307. {
  308.     AEDesc        aTextDesc = { typeNull, NULL },
  309.                 aNullDesc = {typeNull, NULL},
  310.                 aResultDesc = { typeNull, NULL };
  311.     TextToken    aTextToken;
  312.     long        aCount,
  313.                 anIndex;
  314.     DescType    anAEKeyword;
  315.     OSErr        anErr;
  316.  
  317.     
  318.     switch ( theDesc->descriptorType )
  319.     {
  320.         case typeNull:
  321.         case typeScript:
  322.             return noErr;
  323.     
  324.         case typeChar:
  325.         case typeStyledText:
  326.         case typeIntlText:
  327.             anErr = AEDuplicateDesc( theDesc, &aTextDesc );
  328.             break;
  329.             
  330.         case typeObjectSpecifier:
  331.         case cEventIdentifier:
  332.             anErr = TextDescUsingOSADisplay( theDesc, &aTextDesc );
  333.             break;
  334.             
  335.         case typeAEList:
  336.             anErr = AddPStrToResults( "\p{ " );    // Brackets to signify a list
  337.             if ( noErr != anErr ) goto done;
  338.         
  339.             anErr = AECountItems( theDesc, &aCount );
  340.             if ( noErr != anErr ) goto done;
  341.             
  342.             for ( anIndex = 1; anIndex <= aCount; anIndex++ )
  343.             {
  344.                 anErr = AEGetNthDesc( theDesc, anIndex, typeWildCard, &anAEKeyword, &aTextDesc );
  345.                 if ( noErr != anErr ) goto done;
  346.                     // Call recursively - may even be another list
  347.                 anErr = AddDescToResults( &aTextDesc );
  348.                 if ( noErr != anErr ) goto done;
  349.                 
  350.                 (void)AEDisposeDesc( &aTextDesc );
  351.             }
  352.  
  353.             anErr = AddPStrToResults( "\p }" );
  354.             goto done;    // We have been through the list so no further descriptor to add
  355.             
  356.         case typeType:
  357.         case typeEnumeration:
  358.             anErr = AECoerceDesc( theDesc, typeChar, &aTextDesc );
  359.  
  360.             if ( noErr != anErr ) goto done;        // Add the OSType first
  361.             anErr = AddPStrToResults( "\p'" );
  362.             if ( noErr != anErr ) goto done;
  363.             anErr = AddDescToResults( &aTextDesc );
  364.             if ( noErr != anErr ) goto done;
  365.             anErr = AddPStrToResults( "\p'" );
  366.             if ( noErr != anErr ) goto done;
  367.             (void)AEDisposeDesc( &aTextDesc );        // We're using it again
  368.  
  369.             anErr = TextDescUsingOSADisplay( theDesc, &aTextDesc );
  370.             if ( noErr == anErr )
  371.                 anErr = AddPStrToResults( "\p - " );    // Go on to display the terminology
  372.             else
  373.             {                                        // Couldn't get terminology
  374.                 anErr = noErr;                        //  so make do with OSType.
  375.                 goto done;
  376.             }
  377.             break;
  378.             
  379.         default:    // Apple Event Manager can coerce numbers and stuff
  380.             anErr = AECoerceDesc( theDesc, typeChar, &aTextDesc );
  381.     }
  382.     
  383.     if ( noErr != anErr ) goto done;
  384.     
  385.     anErr = EndOfDocumentTextToken( GetResultsWindPtr( ), &aTextToken );
  386.     if ( noErr != anErr ) goto done;
  387.     
  388.         // Add text to end of results window
  389.     anErr = CreateAtTextToken( cText, &aTextDesc, &aTextToken, &aNullDesc, &aResultDesc );
  390.     
  391. done:
  392.     (void)AEDisposeDesc( &aTextDesc );
  393.     (void)AEDisposeDesc( &aResultDesc );
  394.  
  395.     return anErr;
  396. }
  397.  
  398. static OSErr    EndOfDocumentTextToken( WindowPtr theWindow, TextToken* theToken )
  399. {
  400.     DPtr    aDoc = DPtrFromWindowPtr( theWindow );
  401.     
  402.     if ( ! aDoc )
  403.         return errAEWrongDataType;
  404.         
  405.     theToken->tokenWindow = theWindow;
  406.     theToken->tokenOffset = (**aDoc->theText).teLength + 1;
  407.     theToken->tokenLength = 0;
  408.     
  409.     return noErr;
  410. }
  411.  
  412.  
  413. // Routine that takes a type descriptor and returns the text that is 
  414. // used to describe the type in a script.
  415. // e.g. atypeType descriptor for 'docu' will return a text descriptor containing "document"
  416.  
  417. OSAError    TextDescUsingOSADisplay( AEDesc* theDesc, AEDesc* theResult )
  418. {
  419.     OSAID        aScriptID = kOSANullScript;
  420.     OSAError    anErr;
  421.  
  422.                         // Convert from a descriptor to a script ID
  423.     anErr = OSACoerceFromDesc( gScriptingComponent, theDesc, kOSAModeNull, &aScriptID );
  424.     if (noErr != anErr) goto done;
  425.                                                    // typeStyledText
  426.     anErr = OSADisplay( gScriptingComponent, aScriptID, typeChar, kOSAModeNull, theResult );
  427.  
  428. done:                    // Clean up memory
  429.     (void)OSADispose( gScriptingComponent, aScriptID );
  430.  
  431.     return anErr;
  432. }
  433.  
  434.